package J1_20;

import java.util.*;

public class test {

    public class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
    //给你一个链表的头节点 head 和一个整数 val ，请你删除链表中所有满足 Node.val == val 的节点，并返回 新的头节点 。
    public ListNode removeElements(ListNode head, int val) {
        if (head == null) {
            return null;
        }
        ListNode cur = head;

        while (cur.next != null) {
            ListNode curNext = cur.next;
            if (curNext.val == val) {
                cur.next = curNext.next;
            } else {
                cur = curNext;
            }
        }

        if (head.val == val) {
            head = head.next;
        }

        return head;
    }

    //给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。
    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode cur = head;
        ListNode curNext = cur.next;
        cur.next = null;
        while (curNext.next != null) {
            ListNode curNNext = curNext.next;
            curNext.next = cur;
            cur = curNext;
            curNext = curNNext;
        }
        curNext.next = cur;
        return curNext;
    }


}
